home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / com / arpitonline / controls / ScrollImage.as
Encoding:
Text File  |  2008-05-21  |  6.1 KB  |  201 lines

  1. package com.arpitonline.controls
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.MouseEvent;
  5.    import mx.controls.Image;
  6.    import mx.core.ScrollControlBase;
  7.    import mx.core.ScrollPolicy;
  8.    import mx.events.FlexEvent;
  9.    import mx.events.ScrollEvent;
  10.    import mx.events.ScrollEventDirection;
  11.    
  12.    public class ScrollImage extends ScrollControlBase
  13.    {
  14.       protected var _source:String;
  15.       
  16.       private var oldMouseY:Number = NaN;
  17.       
  18.       private var oldMouseX:Number = NaN;
  19.       
  20.       protected var lineScrollSize:int = 10;
  21.       
  22.       private var deltaX:Number;
  23.       
  24.       private var deltaY:Number;
  25.       
  26.       protected var img:Image;
  27.       
  28.       public function ScrollImage()
  29.       {
  30.          lineScrollSize = 10;
  31.          oldMouseX = NaN;
  32.          oldMouseY = NaN;
  33.          super();
  34.          this.horizontalScrollPolicy = ScrollPolicy.AUTO;
  35.       }
  36.       
  37.       public function get source() : String
  38.       {
  39.          return _source;
  40.       }
  41.       
  42.       override protected function mouseWheelHandler(param1:MouseEvent) : void
  43.       {
  44.          if(param1.delta > 1)
  45.          {
  46.             if(img.y + img.height >= this.height)
  47.             {
  48.                return;
  49.             }
  50.          }
  51.          else if(img.y <= 0)
  52.          {
  53.             return;
  54.          }
  55.          super.mouseWheelHandler(param1);
  56.       }
  57.       
  58.       private function onUpdateComplete(param1:Event) : void
  59.       {
  60.          if(!img.content || img.content.width == 0 || img.content.height == 0)
  61.          {
  62.             return;
  63.          }
  64.          img.removeEventListener(FlexEvent.UPDATE_COMPLETE,onUpdateComplete);
  65.          img.width = img.content.width;
  66.          img.height = img.content.height;
  67.          this.setScrollBarProperties(img.width,unscaledWidth,img.height,unscaledHeight);
  68.          if(null != this.horizontalScrollBar)
  69.          {
  70.             this.horizontalScrollBar.lineScrollSize = lineScrollSize;
  71.          }
  72.          if(null != this.verticalScrollBar)
  73.          {
  74.             this.verticalScrollBar.lineScrollSize = lineScrollSize;
  75.          }
  76.          zero();
  77.       }
  78.       
  79.       public function set source(param1:String) : void
  80.       {
  81.          _source = param1;
  82.          invalidateProperties();
  83.       }
  84.       
  85.       private function onScroll(param1:ScrollEvent) : void
  86.       {
  87.          if(param1.direction == ScrollEventDirection.VERTICAL)
  88.          {
  89.             this.img.y = -param1.position;
  90.             if(this.img.y + this.img.height < this.height)
  91.             {
  92.                img.y += this.height - (img.y + img.height);
  93.             }
  94.             if(img.y > 0)
  95.             {
  96.                img.y = 0;
  97.             }
  98.          }
  99.          else
  100.          {
  101.             this.img.x = -param1.position;
  102.             if(img.x + img.width < this.width)
  103.             {
  104.                img.x += this.width - (img.x + img.width);
  105.             }
  106.             if(img.x > 0)
  107.             {
  108.                img.x = 0;
  109.             }
  110.          }
  111.       }
  112.       
  113.       override protected function commitProperties() : void
  114.       {
  115.          super.commitProperties();
  116.          if(!_source || _source == img.source)
  117.          {
  118.             return;
  119.          }
  120.          img.source = _source;
  121.          img.addEventListener(FlexEvent.UPDATE_COMPLETE,onUpdateComplete);
  122.       }
  123.       
  124.       public function set scale(param1:Number) : void
  125.       {
  126.          this.img.width = img.content.width * param1;
  127.          this.img.height = img.content.height * param1;
  128.          this.setScrollBarProperties(img.content.width * param1,unscaledWidth,img.content.height * param1,unscaledHeight);
  129.          if(img.x + img.width < this.width)
  130.          {
  131.             img.x += this.width - (img.x + img.width);
  132.          }
  133.          if(img.y + img.height < this.height)
  134.          {
  135.             img.y += this.height - (img.y + img.height);
  136.          }
  137.       }
  138.       
  139.       public function zero() : void
  140.       {
  141.          img.x = img.y = 0;
  142.          img.buttonMode = true;
  143.          this.horizontalScrollPosition = 0;
  144.          this.verticalScrollPosition = 0;
  145.       }
  146.       
  147.       override protected function createChildren() : void
  148.       {
  149.          super.createChildren();
  150.          img = new Image();
  151.          img.showBusyCursor = true;
  152.          img.includeInLayout = false;
  153.          addChild(img);
  154.          img.mask = this.maskShape;
  155.          this.addEventListener(ScrollEvent.SCROLL,onScroll);
  156.          img.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
  157.          img.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
  158.       }
  159.       
  160.       private function onMouseMove(param1:MouseEvent) : void
  161.       {
  162.          deltaX = -(mouseX - oldMouseX);
  163.          deltaY = -(mouseY - oldMouseY);
  164.          this.horizontalScrollPosition += deltaX;
  165.          this.verticalScrollPosition += deltaY;
  166.          if(deltaX < 0 && img.x < 0)
  167.          {
  168.             dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.horizontalScrollPosition,"horizontal",deltaX));
  169.          }
  170.          else if(deltaX > 0 && img.x + img.width > this.width)
  171.          {
  172.             dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.horizontalScrollPosition,"horizontal",deltaY));
  173.          }
  174.          if(deltaY > 0 && img.y + img.height > this.height)
  175.          {
  176.             dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.verticalScrollPosition,"vertical",deltaY));
  177.          }
  178.          else if(deltaY < 0 && img.y < 0)
  179.          {
  180.             dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.verticalScrollPosition,"vertical",deltaY));
  181.          }
  182.          oldMouseX = mouseX;
  183.          oldMouseY = mouseY;
  184.       }
  185.       
  186.       private function onMouseUp(param1:MouseEvent) : void
  187.       {
  188.          this.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
  189.       }
  190.       
  191.       private function onMouseDown(param1:MouseEvent) : void
  192.       {
  193.          oldMouseX = this.mouseX;
  194.          oldMouseY = this.mouseY;
  195.          this.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
  196.          stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
  197.       }
  198.    }
  199. }
  200.  
  201.